package net.huashitong.threeSeven.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.filter.BaseFilter;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.threeSeven.dao.IndexDataDao;
import net.huashitong.threeSeven.entity.IndexData;
import net.huashitong.threeSeven.entity.JdFinishDetail;
import net.huashitong.threeSeven.filter.IndexDataFilter;
import net.huashitong.threeSeven.filter.RequestModelFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static net.huashitong.threeSeven.util.DataUtils.tdf;
import static org.apache.commons.lang3.StringUtils.trim;

@Service
public class IndexDataService extends StringPKService<IndexData> {

    @Autowired
    private IndexDataDao dao;
    @Autowired
    private SysTableService tableService;
    @Autowired
    private JdFinishDeatilService jservice;

    @Override
    protected BaseDao<IndexData, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(IndexData obj) {
        obj.setSysId(tableService.updateMaxSysId("t_gis_tsIndexData", null));
    }

    public Map<String, Object> getIndex(IndexDataFilter filter) {

        Map<String, Object> result = new HashMap<>();
        List<String> strList = new ArrayList<>();

        Map<String, Object> map1 = getNewDateTime(filter);
        String newDateTime = (String) map1.get("dateTime");
        List<IndexData> list = (List<IndexData>) map1.get("list");

        //封装一级指标
        for (IndexData i : list) {
            if ("0".equals(i.getPreIndexCode()) || ObjectUtils.isBlank(i.getPreIndexCode())) {
                strList.add(i.getIndexCode());
            }
        }
        RequestModelFilter filter1 = new RequestModelFilter();
        filter1.setDateTime(newDateTime);
        List<JdFinishDetail> jList = jservice.queryByTime(filter1);
        Map<String, JdFinishDetail> map = new HashMap<>();
        for (JdFinishDetail detail : jList) {
            if (!map.containsKey(detail.getIndexCode())) {
                map.put(detail.getIndexCode(), detail);
            }
        }

        List<Map<String, Object>> resultList = new ArrayList<>();
        //遍历一级指标编码
        for (String indexCode : strList) {
            Map<String, Object> strMap = new HashMap<>();
            List<Map<String, String>> mapList = new ArrayList<>();
            for (IndexData i : list) {
                if (indexCode.equals(i.getIndexCode())) {
                    strMap.put("indexCode", i.getIndexCode());
                    strMap.put("indexName", i.getIndexName());
                    strMap.put("department", i.getDepartment());
                    //比增百日目标
                    strMap.put("increaseHundredTarget", trim(tdf(i.getIncreaseHundredTarget())));
                    //比增百日目标单位
                    strMap.put("increaseHundredTargetUnit", "%");
                    //总量百日目标
                    strMap.put("totalHundredTarget", trim(tdf(i.getTotalHundredTarget())));
                    //总量百日目标单位
                    strMap.put("totalHundredTargetUnit", i.getTotalUnit());
                    JdFinishDetail jdFinishDetail = map.get(i.getIndexCode());
                    /*实际增长值*/
                    strMap.put("factIncrease", trim(jdFinishDetail.getIncrease()));
                    /*实际增长值单位*/
                    strMap.put("factIncreaseUnit", jdFinishDetail.getIncreaseUnit());
                    /*实际总量*/
                    strMap.put("factTotal", trim(jdFinishDetail.getTotal()));
                    /*实际总量单位*/
                    strMap.put("factTotalUnit", jdFinishDetail.getTotalUnit());

                    if ("A001".equals(i.getIndexCode())) {
                        //比增市目标
                        strMap.put("increaseTarget", trim(tdf(i.getIncreaseTarget())));
                        //总量市目标
                        strMap.put("totalTarget", trim(tdf(i.getTotalTarget())));
//                        strMap.put("increase", i.getIncrease()); //比增
//                        strMap.put("increaseUnit", "%");         //比增单位
//                        strMap.put("total", i.getTotal());       //总量
//                        strMap.put("totalUnit",i.getTotalUnit());//总量单位
                        strMap.put("jdRank", trim(i.getJdRank()));     //季度排名
                    }
                }
                //二级指标
                if (indexCode.equals(i.getPreIndexCode())) {
                    Map<String, String> map2 = new HashMap<>();
                    map2.put("indexCode", i.getIndexCode());
                    map2.put("indexName", i.getIndexName());
                    map2.put("jdRank", trim(i.getJdRank()));         //季度排名
                    map2.put("increase", trim(i.getIncrease()));     //增长
                    map2.put("increaseUnit", "%");              //增长单位
                    map2.put("total", trim(i.getTotal()));           //总量
                    map2.put("totalUnit", i.getTotalUnit());    //总量单位
                    mapList.add(map2);
                }
            }
            if (mapList.size() > 0 && mapList != null) {
                strMap.put("list", mapList);
            }
            resultList.add(strMap);
        }
        result.put("resList", resultList);
        result.put("dateTime",newDateTime);
        return result;
    }

    //web接口
    public Map<String, Object> getWebIndex(IndexDataFilter filter) {
        Map<String, Object> result = new HashMap<>();
        List<String> strList = new ArrayList<>();

        Map<String, Object> map1 = getNewDateTime(filter);
        String newDateTime = (String) map1.get("dateTime");
        List<IndexData> list = (List<IndexData>) map1.get("list");

        //封装一级指标
        for (IndexData i : list) {
            if ("0".equals(i.getPreIndexCode()) || ObjectUtils.isBlank(i.getPreIndexCode())) {
                strList.add(i.getIndexCode());
            }
        }
        RequestModelFilter filter1 = new RequestModelFilter();
        filter1.setDateTime((String) map1.get("dateTime"));
        List<JdFinishDetail> jList = jservice.queryByTime(filter1);
        Map<String, JdFinishDetail> map = new HashMap<>();

        for (JdFinishDetail detail : jList) {
            if (!map.containsKey(detail.getIndexCode())) {
                map.put(detail.getIndexCode(), detail);
            }
        }

        List<Map<String, Object>> resultList = new ArrayList<>();
        //遍历一级指标编码
        for (String indexCode : strList) {
            Map<String, Object> strMap = new HashMap<>();
            List<Map<String, String>> mapList = new ArrayList<>();
            for (IndexData i : list) {
                if (indexCode.equals(i.getIndexCode())) {
                    strMap.put("indexCode", i.getIndexCode());
                    strMap.put("indexName", i.getIndexName());
                    strMap.put("department", i.getDepartment());

                    JdFinishDetail jdFinishDetail = map.get(i.getIndexCode());
                    if(ObjectUtils.isNotBlank(jdFinishDetail)){
                        /*实际增长值*/
                        strMap.put("factIncrease", StringUtils.isBlank(trim(jdFinishDetail.getIncrease()))?
                                "-":trim(jdFinishDetail.getIncrease()));
                        /*实际增长值单位*/
                        strMap.put("factIncreaseUnit", jdFinishDetail.getIncreaseUnit());
                        /*实际总量*/
                        strMap.put("factTotal", StringUtils.isBlank(trim(jdFinishDetail.getTotal()))?
                                "-":trim(jdFinishDetail.getTotal()));
                        /*实际总量单位*/
                        strMap.put("factTotalUnit", jdFinishDetail.getTotalUnit());
                    }else{
                        strMap.put("factIncrease", "-");
                        strMap.put("factIncreaseUnit", "%");
                        strMap.put("factTotal", "-");
                        strMap.put("factTotalUnit", "万元");
                    }


                }
            }
            if (mapList.size() > 0 && mapList != null) {
                strMap.put("list", mapList);
            }
            resultList.add(strMap);
        }
        result.put("resList", resultList);
        result.put("dateTime", newDateTime);
        return result;
    }

    //web接口
    public Map<String, Object> getWebIndexDetail(IndexDataFilter filter) {
        Map<String, Object> result = new HashMap<>();
        String indexCode = filter.getIndexCode();
        filter.setIndexCode("");
        Map<String, Object> map1 = getNewDateTime(filter);
        String newDateTime = (String) map1.get("dateTime");
        List<IndexData> list = (List<IndexData>) map1.get("list");

        RequestModelFilter filter1 = new RequestModelFilter();
        filter1.setDateTime((String) result.get("dateTime"));
        List<JdFinishDetail> jList = jservice.queryByTime(filter1);
        Map<String, JdFinishDetail> map = new HashMap<>();
        for (JdFinishDetail detail : jList) {
            if (!map.containsKey(detail.getIndexCode())) {
                map.put(detail.getIndexCode(), detail);
            }
        }
        List<Map<String, String>> detailList = new ArrayList<>();
        for (IndexData i : list) {
            if (indexCode.equals(i.getPreIndexCode())) {
                //二级指标
                Map<String, String> map2 = new HashMap<>();
                map2.put("indexCode", i.getIndexCode());
                map2.put("indexName", i.getIndexName());
                map2.put("jdRank", trim(i.getJdRank()));         //季度排名
                map2.put("increase", StringUtils.isBlank(trim(i.getIncrease()))?"-":trim(i.getIncrease()));     //增长
                map2.put("increaseUnit", "%");              //增长单位
                map2.put("total", StringUtils.isBlank(trim(i.getTotal()))?"-":trim(i.getTotal()));           //总量
                map2.put("totalUnit", i.getTotalUnit());    //总量单位
                detailList.add(map2);
            }
        }

        result.put("detailList", detailList);
        result.put("dateTime", newDateTime);
        return result;
    }


    public Map<String, Object> getNewDateTime(IndexDataFilter filter) {
        Map<String, Object> result = new HashMap<>();
        String newDateTime;
        List<IndexData> list;
        if (ObjectUtils.isBlank(filter.getDateTime())) {
            //时间为空，则获取最新时间
            newDateTime = dao.getNewDateTime();
            filter.setDateTime(newDateTime);
            list = dao.getIndex(filter);
            result.put("dateTime", newDateTime);
        } else {
            list = dao.getIndex(filter);
            result.put("dateTime", filter.getDateTime());
        }
        result.put("list", list);
        return result;
    }

    //获取原始数据
    public List<IndexData> getIndexData(IndexDataFilter filter)
    {
        return dao.getIndex(filter);
    }


}
